import os

from yl_pms import utils


def rm_cache():
    """删除缓存"""
    cache_dir = 'pms/migrations/'
    init_file = '__init__.py'
    li = os.listdir(cache_dir)
    for f in li:
        file_path_rm = cache_dir + f
        if f != init_file and os.path.isfile(file_path_rm):
            os.remove(file_path_rm)
    msg = 'cache rm success'
    utils.print_info(msg)


def update(pc):
    # 创建数据迁移模型
    cmd1 = 'python3 manage.py makemigrations --settings=yl_pms.settings.settings_{}'.format(pc)
    os.system(cmd1)
    msg = 'virtual model update success'
    utils.print_info(msg)

    # 迁移到真实的数据库
    cmd2 = 'python3 manage.py migrate --settings=yl_pms.settings.settings_{}'.format(pc)
    os.system(cmd2)
    msg = 'real model update success'
    utils.print_info(msg)


if __name__ == '__main__':
    """
    1、该脚本需要和manage.py在同一目录；
    2、该脚本用户同步数据库模型，修改models中的模型，数据库会自动同步；
    """
    project = 'p1'
    # rm_cache()
    update(project)
